import tkinter as tk
from tkinter import scrolledtext
import pymssql

def open(s):
    window = tk.Tk()
    window.title("养护人员-" + s + "---删除")
    window.geometry("400x300")
#设置列的权重
    window.grid_columnconfigure(0, weight=1)
    window.grid_columnconfigure(1, weight=2)
    window.grid_columnconfigure(2, weight=1)

    label1 = tk.Label(window, text="请输入要删除的养护任务编号：")
    entry1 = tk.Entry(window)

    conn = pymssql.connect(host='localhost', server='DESKTOP-NBNT8AG', port='1433', user='lxy',
                           password='lxy618',
                           database='植物管理系统', charset='utf8')
    if conn:
        print("连接成功！")

    def check2():
        message = entry1.get()
        cursor = conn.cursor()  # 获取游标

        try:
            sql = f"DELETE FROM 养护 WHERE 养护.养护任务编号 = '{message}'"
            cursor.execute(sql)
            if cursor.rowcount == 0:
                scrollbar.insert(tk.INSERT, "不存在编号为" + message + "的养护任务！")
            else:
                conn.commit()
                scrollbar.insert(tk.INSERT, "已成功删除编号为" + message + "的养护任务！")

        except Exception as e:
            scrollbar.insert(tk.INSERT, "删除失败，原因如下：" + str(e))
            conn.rollback()

    button1 = tk.Button(window, text="删除养护任务", command=lambda: check2(), width=20, height=1)
    return_button = tk.Button(window, text="返回", command=window.destroy, width=20, height=1)

    label1.grid(row=0, column=0)
    entry1.grid(row=1, column=0)

    button1.grid(row=2, column=0)
    return_button.grid(row=3, column=1, sticky="nsew")
    scrollbar = scrolledtext.ScrolledText(window, wrap=tk.WORD, width=60, height=10)
    scrollbar.grid(row=5, column=0, columnspan=4, sticky="nsew")
    window.mainloop()
    pass